package com.example.smallandroidcodetester;

import java.util.TimerTask;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.widget.TextView;

public class CustomProgressDialog extends Dialog {
	private static final long DIALOG_TIMEOUT = 1000 * 20;
	private Handler mHandler = new Handler();
	private TextView text;
	private TimerTask task;

	public CustomProgressDialog(Context context, String message) {
		super(context);
		setContentView(R.layout.progress_dialog);
		setCancelable(false);
		// text = (TextView) findViewById(R.id.ProgressText);
		if (message != null) {
			text.setText(message);
		}
		task = new TimerTask() {
			@Override
			public void run() {
				dismiss();
			}
		};
	}

	public CustomProgressDialog(Context context) {
		this(context, null);
	}

	@Override
	public void hide() {
		mHandler.removeCallbacks(task);
		if (task != null) {
			task.cancel();
		}
		task = null;
		dismiss();
	}

	public void setMessage(String message) {
		if (text != null) {
			text.setText(message);
		}
	}

	public String getMessage() {
		return text.getText().toString();
	}

	public void startDialogCollector(String name) {
		mHandler.removeCallbacks(task);
		mHandler.postDelayed(task, DIALOG_TIMEOUT);
	}
}
